package com.guigu.lc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.mapper.*;
import com.guigu.common.pojo.*;
import com.guigu.common.vo.SearchVo;
import com.guigu.lc.service.materialService;
import com.guigu.lc.view.desginmaterview;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 产品物料设计，审核，查询，变更的Controller
 */
@Service
public class materialServiceimpl  implements materialService {

    @Resource
    private MDesignProcedureMapper designProcedureMapper;

    @Resource
    private DesignModuleMapper designModuleMapper;

    @Resource
    private DModuleDetailsMapper dModuleDetailsMapper;

    @Resource
    private MDesignProcedureModuleMapper mDesignProcedureModuleMapper;

    @Resource
    private MDesignProcedureDetailsMapper mdsmapper;

    /**
     * #查询制定物料设计单
     * @param searchVo
     * @return
     */
    @Override
    public SearchVo<MDesignProcedure> querymaterialldata(SearchVo<MDesignProcedure> searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(),searchVo.getPageSize());
        PageInfo page=new PageInfo(this.designProcedureMapper.querymaterno());
        SearchVo<MDesignProcedure> vo=new SearchVo<MDesignProcedure>();
        vo.setTotal(page.getTotal());
        vo.setRows(page.getList());
        return vo;
    }

    /**
     *#查询物料设计审核 查询待审核工序物料设计
     * @param searchVo
     * @return
     */
    @Override
    public SearchVo<MDesignProcedure> querycheckmaterService(SearchVo<MDesignProcedure> searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(),searchVo.getPageSize());
        PageInfo page=new PageInfo(this.designProcedureMapper.querycheckmater());
        SearchVo<MDesignProcedure> vo=new SearchVo<MDesignProcedure>();
        vo.setTotal(page.getTotal());
        vo.setRows(page.getList());
        return vo;
    }

    /**
     *#查询所有登记的工序物料设计单 查询物料设计不是未设计的工序设计单
     * @param searchVo
     * @return
     */
    @Override
    public SearchVo<MDesignProcedure> queryallsymaterService(SearchVo<MDesignProcedure> searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(),searchVo.getPageSize());
        PageInfo page=new PageInfo(this.designProcedureMapper.queryallsymater());
        SearchVo<MDesignProcedure> vo=new SearchVo<MDesignProcedure>();
        vo.setTotal(page.getTotal());
        vo.setRows(page.getList());
        return vo;
    }

    /**
     * 变更查询
     * @param searchVo
     * @return
     */
    @Override
    public SearchVo<MDesignProcedure> querycallmatertrueService(SearchVo<MDesignProcedure> searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(),searchVo.getPageSize());
        PageInfo page=new PageInfo(this.designProcedureMapper.querycallmatertrue());
        SearchVo<MDesignProcedure> vo=new SearchVo<MDesignProcedure>();
        vo.setTotal(page.getTotal());
        vo.setRows(page.getList());
        return vo;
    }

    @Override
    public DesignModule queryByproductidService(String id) {
        return this.designModuleMapper.queryByproductid(id);
    }

    @Override
    public List<DModuleDetails> queryByidproductidService(int id) {
        return this.dModuleDetailsMapper.queryByidproductid(id);
    }

    @Override
    public List<MDesignProcedureModule> queryByparentidService(int id) {
        return this.mDesignProcedureModuleMapper.queryByparent(id);
    }

    @Override
    public void updateshejiService(desginmaterview desginmaterview) {
        this.mdsmapper.updateByidfromtag(desginmaterview);
        this.mDesignProcedureModuleMapper.insertmodule(desginmaterview);
        this.dModuleDetailsMapper.updateresidByname(desginmaterview);
    }

    @Override
    public void updatecxshejiService(desginmaterview desginmaterview) {
        this.dModuleDetailsMapper.updatexiugai(desginmaterview);
        this.mDesignProcedureModuleMapper.deletecxshejimodulem(desginmaterview.getDetails().get(0).getParentId());
        this.mdsmapper.updatecxsjcpmx(desginmaterview.getDetails().get(0).getParentId());
    }

    @Override
    public int checkgxshifoushejiService(int id) {
        return this.mdsmapper.queryBYoarentyisheji(id);
    }

    @Override
    public List<MDesignProcedureDetails> querywuiaoxiaojiService(int id) {
        return this.mdsmapper.querywuiaoxiaoji(id);
    }

    @Override
    public void updatewuliaozcbdshService(MDesignProcedure procedure) {
        this.designProcedureMapper.updatewuliaozcbdsh(procedure);
    }

    @Override
    public void updateshenghefalse(desginmaterview desginmaterview) {
        this.designProcedureMapper.updatewuliaoshejifalsed(desginmaterview);
        this.mdsmapper.pliangupdatewuliaoshejifalse(desginmaterview);
        this.mDesignProcedureModuleMapper.piliangdeletefromparentfalse(desginmaterview);
        this.dModuleDetailsMapper.updatexiugai1shulian(desginmaterview);
    }

    @Override
    public void updateshengheokkService(desginmaterview desginmaterview) {
        this.designProcedureMapper.updatewuliaoshejifokk(desginmaterview);
    }

    @Override
    public int queryamout0Service(int id) {
        return this.dModuleDetailsMapper.queryamountdengyu0(id);
    }

    @Override
    public MDesignProcedureDetails queymdpdidService(int id) {
        return this.mdsmapper.queryByid(id);
    }

    @Override
    public void deleteandupdatechangestartService(desginmaterview desginmaterview) {
        this.mDesignProcedureModuleMapper.deletecxshejimodulem(desginmaterview.getId());
        this.mdsmapper.updatechangetag1(desginmaterview.getId());
        //根据产品编号找到已设计的产品物料组成
        DesignModule designModule = this.designModuleMapper.queryByproductid(desginmaterview.getCpbh());
        //根据产品物料组成的父级编号和产品名称加数量
        desginmaterview.setWuliaoczid(designModule.getId());

        this.dModuleDetailsMapper.updateshuliangjiahuiq(desginmaterview);
    }

    @Override
    public List<DModuleDetails> selectwuliaozcmxService(String id) {
        //根据产品编号找到已设计的产品物料组成
        DesignModule designModule = this.designModuleMapper.queryByproductid(id);
      return  this.dModuleDetailsMapper.queryByidproductid(designModule.getId());
    }

    @Override
    public void biangengtijiaoService(desginmaterview desginmaterview) {
        //修改物料小计
        this.mdsmapper.updatewuliaoxiaojiye(desginmaterview);
        //插入物料明细表
        this.mDesignProcedureModuleMapper.insertmodule(desginmaterview);
        //根据产品编号找到已设计的产品物料组成
        DesignModule designModule = this.designModuleMapper.queryByproductid(desginmaterview.getCpbh());
        //根据产品物料组成的父级编号和产品名称减数量
        desginmaterview.setWuliaoczid(designModule.getId());
        this.dModuleDetailsMapper.updateaoutjianshuliang(desginmaterview);
    }

    @Override
    public int chaxdengyu0(String id) {
        DesignModule designModule = this.designModuleMapper.queryByproductid(id);
        int q = this.dModuleDetailsMapper.queryamountdengyu0(designModule.getId());
        return q;
    }

    @Override
    public void cxtxbgdasService(String id) {
        MDesignProcedure querydesidall = this.designProcedureMapper.querydesidall(id);
        List<MDesignProcedureDetails> querywuiaoxiaoji = this.mdsmapper.querywuiaoxiaoji(querydesidall.getId());
        int sum=0;
        for (MDesignProcedureDetails z:querywuiaoxiaoji ){
            sum+=z.getModuleSubtotal();
        }
        MDesignProcedure x=new MDesignProcedure();
        x.setDesignId(id);
        x.setModuleCostPriceSum(sum);
        this.designProcedureMapper.updatwwuliaoxiaoji(x);
    }


}
